//
//  StrokeRecord.m
//  StrokeDemo
//
//  Created by wankang on 2/16/15.
//  Copyright (c) 2015 wankang. All rights reserved.
//

#import "StrokeRecord.h"

static float EPSILON = 1.0e-5f;

@implementation StrokeRecord
@synthesize code = _code;
@synthesize indexContour = _indexContour;
@synthesize nameStroke = _nameStroke;
@synthesize xStart = _xStart;
@synthesize yStart = _yStart;
@synthesize type = _type;
@synthesize endStroke = _endStroke;
@synthesize endAngle = _endAngle;
@synthesize arrContour = _arrContour;
@synthesize arrMiddle = _arrMiddle;

-(id)init
{
    if (self = [super init]) {
        _code = nil;
        _nameStroke = nil;
        _indexContour = -1;
        _xStart = 0;
        _yStart = 0;
        _type = 0;
        _endStroke = NO;
        _endAngle = 0;
        _arrContour = nil;
        _arrMiddle = nil;
    }
    return self;
}

-(void)dealloc
{
    _code = nil;
    _nameStroke = nil;
    _arrContour = nil;
    _arrMiddle = nil;
}

-(BOOL)isBuShou
{
    return (_type & 0x01) != 0;
}

-(BOOL)isStrokeEnd
{
    return (_type & 0x02) == 0;
}

-(BOOL)getContour:(StrokePoint *)pt fromIndex:(NSInteger)index
{
    if (index<0 || _arrContour==nil || index>=_arrContour.count)
        return NO;
    NSNumber *value = [_arrContour objectAtIndex:index];
    pt->x = (value.intValue & 0xffff);
    pt->y = ((value.intValue >> 16) & 0xffff);
    return YES;
}

-(BOOL)getMidline:(StrokePoint *)pt fromIndex:(NSInteger)index
{
    if (index<0 || _arrMiddle==nil || index>=_arrMiddle.count)
        return NO;
    NSNumber *value = [_arrMiddle objectAtIndex:index];
    pt->x = (value.intValue & 0xffff);
    pt->y = ((value.intValue >> 16) & 0xffff);
    return YES;
}

-(StrokeRecord *)createRecordWithXScale:(float)fX andYScale:(float)fY
{
    StrokeRecord *result = [[StrokeRecord alloc] init];
    if (result == nil)
        return nil;
    result.code = self.code;
    result.indexContour = _indexContour;
    result.nameStroke = self.nameStroke;
    result.type = _type;
    result.endStroke = _endStroke;
    result.endAngle = _endAngle;
//    result.xStart = (NSInteger)(_xStart * fX + EPSILON);
//    result.yStart = (NSInteger)(_yStart * fY + EPSILON);
        result.xStart = (NSInteger)(_xStart * fX + 0.5f);//telen
        result.yStart = (NSInteger)(_yStart * fY + 0.5f);
    result.arrContour = [StrokeRecord createScaledPoint:_arrContour withXScale:fX andYScale:fY];
    result.arrMiddle = [StrokeRecord createScaledPoint:_arrMiddle withXScale:fX andYScale:fY];
    return result;
}

+(NSArray *)createScaledPoint:(NSArray *)array withXScale:(float)fXScale andYScale:(float)fYScale
{
    if (array==nil || array.count==0)
        return nil;
    int nCount = (int)array.count;
    NSMutableArray *arr1 = [[NSMutableArray alloc] initWithCapacity:nCount];
    for (int m=0; m<nCount; m++) {
        NSNumber *value = [array objectAtIndex:m];
        short x = (short)(value.intValue & 0xffff);
        short y = (short)((value.intValue >> 16) & 0xffff);
//        short x1 = (short)(x * fXScale + EPSILON);
//        short y1 = (short)(y * fYScale + EPSILON);
        short x1 = (short)(x * fXScale + 0.5f);//telen
        short y1 = (short)(y * fYScale + 0.5f);
        NSNumber *value2 = [ [NSNumber alloc] initWithInt:x1+(y1<<16) ];
        [arr1 addObject:value2];
    }
    return arr1;
}
@end
